package com.ruoyi.web.controller.wms;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.wms.domain.WmsMaterialInfo;
import com.ruoyi.wms.domain.WmsOutCheckItem;
import com.ruoyi.wms.service.IWmsMaterialInfoService;
import com.ruoyi.wms.service.IWmsOutCheckItemService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wms.domain.WmsOutCheckHead;
import com.ruoyi.wms.service.IWmsOutCheckHeadService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 出库Controller
 * 
 * @author ruoyi
 * @date 2024-02-15
 */
@Controller
@RequestMapping("/wms/outhead")
public class WmsOutCheckHeadController extends BaseController
{
    private String prefix = "wms/outhead";

    @Autowired
    private IWmsOutCheckHeadService wmsOutCheckHeadService;

    @Autowired
    private IWmsOutCheckItemService wmsOutCheckItemService;

    @Autowired
    private IWmsMaterialInfoService wmsMaterialInfoService;

    @Autowired
    private ISysConfigService configService;


    @GetMapping()
    public String outhead()
    {
        return prefix + "/outhead";
    }
    /**
     * 查询出库列表
     */
    @RequiresPermissions("wms:outhead:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WmsOutCheckHead wmsOutCheckHead)
    {
        startPage();
        if(!ShiroUtils.getSysUser().isAdmin()){
            wmsOutCheckHead.setDeptId(ShiroUtils.getSysUser().getDeptId());
        }
        List<WmsOutCheckHead> list = wmsOutCheckHeadService.selectWmsOutCheckHeadList(wmsOutCheckHead);
        return getDataTable(list);
    }

    /**
     * 导出出库列表
     */
    @RequiresPermissions("wms:outhead:export")
    @Log(title = "出库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WmsOutCheckHead wmsOutCheckHead)
    {
        List<WmsOutCheckHead> list = wmsOutCheckHeadService.selectWmsOutCheckHeadList(wmsOutCheckHead);
        ExcelUtil<WmsOutCheckHead> util = new ExcelUtil<WmsOutCheckHead>(WmsOutCheckHead.class);
        return util.exportExcel(list, "出库数据");
    }

    /**
     * 新增出库
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("userName", ShiroUtils.getSysUser().getUserName());
        mmap.put("createTime", DateUtils.parseDateToStr("yyyy-MM-dd",new Date()));
        mmap.put("code", getCode("CK"));
        return prefix + "/add";
    }
    private String getCode(String code){
        int num =  Integer.parseInt(configService.selectConfigByKey("ck.code")) ;
        SysConfig config = new SysConfig();
        config.setConfigKey("ck.code");
        config.setConfigValue((++num)+"");
        configService.updateConfigValue(config);
        return StringUtils.getCode(code ,num);
    }
    /**
     * 新增保存出库
     */
    @RequiresPermissions("wms:outhead:add")
    @Log(title = "出库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WmsOutCheckHead wmsOutCheckHead)
    {
        return toAjax(wmsOutCheckHeadService.insertWmsOutCheckHead(wmsOutCheckHead));
    }

    /**
     * 修改出库
     */
    @RequiresPermissions("wms:outhead:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WmsOutCheckHead wmsOutCheckHead = wmsOutCheckHeadService.selectWmsOutCheckHeadById(id);
        WmsOutCheckItem item = new WmsOutCheckItem();
        item.setHeadId(wmsOutCheckHead.getId());
        wmsOutCheckHead.setItemList(wmsOutCheckItemService.selectWmsOutCheckItemList(item));
        mmap.put("wmsOutCheckHead", wmsOutCheckHead);
        return prefix + "/edit";
    }

    @RequiresPermissions("wms:outhead:check")
    @Log(title = "出库", businessType = BusinessType.UPDATE)
    @PostMapping("/check")
    @ResponseBody
    public AjaxResult check(WmsOutCheckHead wmsOutCheckHead)
    {
        WmsOutCheckHead head = new WmsOutCheckHead();
        head.setStatus(wmsOutCheckHead.getStatus());
        head.setId(wmsOutCheckHead.getId());
        head.setUpdateBy(ShiroUtils.getSysUser().getUserName());
        head.setUpdateTime(new Date());
        //出库后，减库存
        for(WmsOutCheckItem item:wmsOutCheckHead.getItemList()){
            WmsMaterialInfo m = new WmsMaterialInfo();
            m.setCode(item.getCode());
            m.setTotal(new BigDecimal("-"+item.getOutQuantity().toString()));
            wmsMaterialInfoService.updateWmsMaterialTotal(m);
        }
        return toAjax(wmsOutCheckHeadService.updateWmsOutCheckHead(head));
    }
    /**
     * 修改保存出库
     */
    @RequiresPermissions("wms:outhead:edit")
    @Log(title = "出库", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WmsOutCheckHead wmsOutCheckHead)
    {
        return toAjax(wmsOutCheckHeadService.updateWmsOutCheckHead(wmsOutCheckHead));
    }

    /**
     * 删除出库
     */
    @RequiresPermissions("wms:outhead:remove")
    @Log(title = "出库", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wmsOutCheckHeadService.deleteWmsOutCheckHeadByIds(ids));
    }
}
